package com.origin.onlinesys.service.modules.user.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.origin.onlinesys.common.services.BaseService;
import com.origin.onlinesys.domain.modules.interviewsys.Account;
import com.origin.onlinesys.persistence.modules.sys.account.AccountMapper;
import com.origin.onlinesys.service.modules.user.IAccountService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/2/7 0007.
 */
@Service
public class AccountServiceImpl extends BaseService<Account> implements IAccountService{
    /**
     * 插入用户
     * @param account
     * @return
     */
    public Account insertAccount(Account account){
        account.setState("1");
        this.save(account);
        return account;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    public int deleteAccount(String id){
        Account account = new Account();
        account.setId(id);
        account.setState("2");
        return this.update(account);
    }

    /**
     * 修改用户信息
     * @param account
     * @return
     */
    public int updateAccount(Account account){
        return this.update(account);
    }

    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    public  Account selectByUserNameAndPassword(String username, String password){
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return this.selectSingleByWhere(account);
    }


    @Override
    public Account selectByUserName(String username) {
        Account account = new Account();
        account.setUsername(username);
        return super.selectSingleByWhere(account);
    }

    /**
     * 根据电话号码查询是否有数据
     * @param phoneNum
     * @return
     */
    public String selectByPhoneNum(String phoneNum){
        return ((AccountMapper)mapper).selectByPhoneNum(phoneNum)!=null?
                ((AccountMapper)mapper).selectByPhoneNum(phoneNum):"0";
    }

    /**
     * 根据Id查询数据
     * @param id
     * @return
     */
    public Account selectById(String id){
        return ((AccountMapper)mapper).selectById(id);
    }

    /**
     * 分页查询出数据
     * @param pageNum
     * @param pageSize
     * @param account
     * @return
     */
//    public Map selectAccountByPage(int pageNum,int pageSize,Account account ){
//        Map map = new HashMap();
//        account.setState("1");
//        Page<Account> page = this.selectByPage(pageNum,pageSize,account);
//        map.put("list", page);
//        map.put("pageSize", page.getPages());
//        map.put("total", page.getTotal());
//        return map;
//    }

    /**
     * 自定义sql分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    public Map selectByPage(int pageNum, int pageSize, String name,String zoneId) {
        Map result = new HashMap();
        Map map = new HashMap();
        map.put("name",name);
        map.put("zoneId",zoneId);
        PageHelper.startPage(pageNum, pageSize, true);
        Page<Account> page = (Page)((AccountMapper)mapper).selectByPage(map);
        result.put("list", page);
        result.put("pageSize", page.getPages());
        result.put("total", page.getTotal());
        return result;
    }

    @Override
    public int addUserRole(String rId, String uId) {
        Map map = new HashMap();map.put("rId",rId);map.put("uId",uId);
        ((AccountMapper)mapper).addUserRole(map);
        return 1;
    }

    @Override
    public int updateUserRole(String rId, String uId) {
        Map map = new HashMap();map.put("rId",rId);map.put("uId",uId);
        ((AccountMapper)mapper).updateUserRole(map);
        return 1;
    }

    @Override
    public List<Account> selectUserByRole(String zoneId) {
        return ((AccountMapper)mapper).selectUserByRole(zoneId);
    }

    @Override
    public List<Account> selectUserByRoleAndJobState(String zoneId) {
        return ((AccountMapper)mapper).selectUserByRoleAndJobState(zoneId);
    }


    /**
     * 修改密码
     * @param userName
     * @param pwd
     * @param newPwd
     * @return
     */
    public String updatePwd(String userName,String pwd, String newPwd) {
        Map map = new HashMap();
        map.put("userName",userName);
        map.put("pwd",pwd);
        String id = ((AccountMapper)mapper).selectUser(map);

        map = new HashMap();
        map.put("pwd",newPwd);
        map.put("id",id);
        ((AccountMapper)mapper).updatePwd(map);
        return id;
    }

    public Account selectInfo(String id) {
        return ((AccountMapper)mapper).selectInfo(id);
    }

    public List<Account> selectItem() {
        return ((AccountMapper)mapper).selectItem();
    }


}
